﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Phone.BackgroundAudio;
using System.Collections.Generic;
using System.Windows.Threading;
using System.Threading;
using DoubanRadio;

namespace AudioPlaybackAgent
{
    public static class AudioHelper
    {
        //public static string url = string.Empty;

        private static int currentIndex = 0;

        private static string fileName = "song.txt";

        private static DoubanHelper DoubanHelper = new DoubanHelper();

        public static List<AudioTrack> PlayList;

        public static void PlayNow(this BackgroundAudioPlayer player)
        {
            if (PlayList == null || PlayList.Count == 0)
            {
                string text = ReadFile(fileName);
                PlayList = DoubanHelper.GetDataFromString(text);
            }
            else
            {
                player.Track = PlayList[currentIndex];
                player.Play();
            }
        }


        public static void PlayPreTrack(this BackgroundAudioPlayer player)
        {
            if (PlayList == null || PlayList.Count == 0)
            {
                string text = ReadFile(fileName);
                PlayList = DoubanHelper.GetDataFromString(text);
            }
            else
            {
                currentIndex--;
                if (currentIndex <=0)
                {
                    currentIndex = PlayList.Count - 1;
                }
                player.Track = PlayList[currentIndex];
                player.Play();
            }

        }

        public static void PlayNextTrack(this BackgroundAudioPlayer player)
        {
            if (PlayList == null || PlayList.Count == 0)
            {
                string text = ReadFile(fileName);
                PlayList = DoubanHelper.GetDataFromString(text);
            }
            currentIndex++;
            if (currentIndex >= PlayList.Count)
            {
                currentIndex = 0;
            }
            player.Track = PlayList[currentIndex];
            player.Play();

        }

        public static void InitPlayList(this BackgroundAudioPlayer player, string url)
        {
            List<AudioTrack> at = new List<AudioTrack>();
            if (!string.IsNullOrEmpty(url))
            {
                Uri uri = new Uri(url, UriKind.Absolute);
                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.BeginGetResponse(r =>
                {
                    var httpRequest = (HttpWebRequest)r.AsyncState;
                    var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);
                    using (var reader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var response = reader.ReadToEnd();
                        Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            WriteFile(fileName, response.ToString());
                            PlayList = DoubanHelper.GetDataFromString(response.ToString());
                            player.Track = PlayList[currentIndex];
                            player.Play();
                        }));
                    }
                }, request);
            }
        }

        private static void WriteFile(string fileName, string content)
        {
            using (var myFileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication()))
            using (var writer = new StreamWriter(myFileStream))
            {
                writer.Write(content);
            }
        }

        private static string ReadFile(string fileName)
        {
            using (var myFileStrem = new IsolatedStorageFileStream(fileName, FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication()))
            using (var reader = new StreamReader(myFileStrem))
            {
                return reader.ReadToEnd().ToString();
            }
        }



    }
}
